defmodule Mps do
  @moduledoc """
  Milis Paket Sistemi
  """

  @doc """
		
     Mps hakkında bilgi verir.
      
      iex> Mps.nedir

  """
  def nedir do
    "MPS Milis Linux için yazılmış paket yönetim uygulamasıdır. Bu konsol, Elixir dilinde üst seviye etkileşim sağlamak için geliştirilmektedir."
  end
  
  @doc """
		
     ilgili talimat hakkında bilgi verir.
      
      iex> Mps.bilgi talimat_ismi

  """
  def bilgi(talimat) do
	case File.read(talimat) do
		{:ok, context}      -> context
		{:error, reason}    -> reason
	end 
  end
  
  def parse_talimat(talimat,key \\ "Gerekler") do
	IO.puts "..." <> talimat
	Enum.map(read_talimat(talimat), \
	fn (lines) -> if String.contains?(lines, key), \
	do: String.split(lines, ":", trim: true) |> tl end)
	|> Enum.filter(& !is_nil(&1)) |> hd
	|> (fn (list) -> if Enum.empty?(list), \
	do: [], else: \
	list |> hd |> String.replace(","," ") |> String.split end).()
	
  end
  
  def deps(talimat) do
    unless :ets.info(:deps) == :undefined , do: :ets.delete(:deps)
    #:ets.delete_all_objects(:deps)
    talimat |> find_deps_chain
  end
  
  
  defp find_deps_chain(talimat) do
    if :ets.info(:deps) == :undefined, do: :ets.new(:deps, [:bag, :set, :public, :named_table])
    #unless :ets.member(:deps,talimat) do 
    if :ets.match(:deps, {:"$1", talimat}) == [] do 
      depends=talimat
      |> parse_talimat
	  talimat |> add_dep
      depends |> Enum.map(&find_deps_chain/1)
    end
    #:ets.tab2list(:deps) 
    #for {pkt} <- :ets.tab2list(:deps)  ,do: pkt
    for {durum,pkt} <- :ets.tab2list(:deps)  ,do: [durum,pkt]
  end
  
  defp add_dep(talimat) do
    #:ets.insert(:deps,{talimat})
    
    case talimat |> kurulu? do
      true  -> :ets.insert(:deps,{"kurulu", talimat})
      false -> :ets.insert(:deps,{"degil", talimat})
    end
    
    #IO.puts "..." <> talimat
  end
  
  def read_talimat(talimat) do
	talimat
	|> find_talimat
	|> File.stream! 
	|> Stream.map(&String.trim_trailing/1) 
	|> Enum.to_list
  end
  
  def kurulu?(paket) do
    db="/var/lib/pkg/DB"
    File.exists?(db <> "/" <> paket)
  end
  
  def talimatlar() do
	Path.wildcard("/root/talimatname/genel/*/*/talimat") |> Enum.map(&Path.dirname(&1)) |> Enum.map(&Path.basename(&1))
  end
  
  def find_talimat(talimat) do
    (Path.wildcard("/root/talimatname/**/" <> talimat) |> hd) <> "/talimat"
  end
  
  def bul(talimat) do
    talimatname_genel="/root/talimatname/genel"
    talimatname_genel <> "/" <> String.at(talimat,0) <> "/" <> talimat <> "/" <> "talimat" |> File.read!
  end
  
end

#Enum.map(lines,depends_control(lines))
#Enum.map(lines,packager_control(lines))
#|> Enum.map(&depends_control(&1))
#|> Enum.map(&packager_control(&1))


# |> Enum.filter(& !is_nil(&1))
# |> Enum.filter(fn(x) -> if String.contains?(x, ["#"]), do: "found: #{x}", else: "not found"  end)
# |> Enum.filter(fn(x) -> if String.contains?(x, ["foo"]), do: x, else: "not found"  end)
# Enum.each(seasons, &IO.puts/1)
# |> Stream.map(fn ({line, index}) -> IO.puts "#{index + 1} #{line}" end)
# |> Stream.map(fn (line) -> IO.puts "#{line}" end)
# do: IO.puts("|_" <> talimat) , else: list |> Enum.each(fn x -> {IO.puts("*" <> x <> "*"), find_deps(x)} end) end).() 
